package com.etang.a2020d1021.fragment;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.etang.a2020d1021.R;
import com.etang.a2020d1021.tools.AppConfig;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BarActivity extends FragmentActivity {

    ViewPager pager;
    PagerChecker pagerChecker;
    List<Fragment> list_fragment = new ArrayList<>();
    RadioButton ra_base, ra_control, ra_value, ra_link;
    TextView tv_time, tv_time_sx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bar);
        tv_time = (TextView) findViewById(R.id.tv_bar_time);
        tv_time_sx = (TextView) findViewById(R.id.tv_bar_time_sx);
        ra_base = (RadioButton) findViewById(R.id.ra_base);
        ra_control = (RadioButton) findViewById(R.id.ra_control);
        ra_value = (RadioButton) findViewById(R.id.ra_value);
        ra_link = (RadioButton) findViewById(R.id.ra_link);
        list_fragment.add(new BaseFragment());
        list_fragment.add(new ControlFragment());
        list_fragment.add(new ValuesFragment());
        list_fragment.add(new LinkFragment());
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setHorizontalScrollBarEnabled(true);
        pagerChecker = new PagerChecker(getSupportFragmentManager());
        pager.setAdapter(pagerChecker);
        ra_value.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(2);
            }
        });
        ra_control.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(1);
            }
        });
        ra_base.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(0);
            }
        });
        ra_link.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pager.setCurrentItem(3);
            }
        });
        pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position) {
                    case 0:
                        ra_base.setChecked(true);
                        break;

                    case 1:
                        ra_control.setChecked(true);
                        break;
                    case 2:
                        ra_value.setChecked(true);
                        break;
                    case 3:
                        ra_link.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        ra_base.setChecked(true);
                        break;
                    case 1:
                        ra_control.setChecked(true);
                        break;
                    case 2:
                        ra_value.setChecked(true);
                        break;
                    case 3:
                        ra_link.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.e("TAG", "onPageScrollStateChanged: " + state);
            }
        });
        handler.post(time);
        AppConfig.server_link(BarActivity.this);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH");
            tv_time.setText(simpleDateFormat.format(new Date()));
            if (Integer.valueOf(simpleDateFormat1.format(new Date())) > 12) {
                tv_time_sx.setText("上午");
            } else {
                tv_time_sx.setText("下午");
            }
            handler.postDelayed(time, 1000);
        }
    };
    Runnable time = new Runnable() {
        @Override
        public void run() {
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };

    public class PagerChecker extends FragmentPagerAdapter {

        public PagerChecker(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list_fragment.get(position);
        }

        @Override
        public int getCount() {
            return list_fragment.size();
        }
    }

}